﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using TranslatorService;

namespace MicrosoftTerminologyService
{
	#pragma warning disable 1998
	public class MicrosoftTerminologyService : ITranslatorService
	{
		// API Docs: http://www.microsoft.com/Language/en-US/Microsoft-Terminology-API.aspx

		private MSTermSvc.TerminologyClient service;
		private MSTermSvc.TranslationSources sources;

		public MicrosoftTerminologyService()
		{
			service = new MSTermSvc.TerminologyClient();
			sources = new MSTermSvc.TranslationSources();
			//sources.Add(MSTermSvc.TranslationSource.Terms);
			sources.Add(MSTermSvc.TranslationSource.UiStrings);
		}

		public TranslatorService.TranslatorServiceCapabilities ServiceCapabilities
		{
			get
			{
				return TranslatorServiceCapabilities.PlainText | TranslatorServiceCapabilities.TranslatesWords | TranslatorServiceCapabilities.TranslatesPhrases;
			}
		}

		public string ServiceName
		{
			get { return "Microsoft Terminology Service"; }
		}

		public Uri ServiceURI
		{
			get { return new Uri("http://api.terminology.microsoft.com/Terminology.svc"); }
		}

		public string[] AuthenticationParameterDetail
		{
			get { return null; }
		}

		public async Task<bool> AuthenticateAsync(params string[] options)
		{
			return true;
		}

		public async Task<Dictionary<string, string>> GetSupportedLanguagesAsync(string locale = "en")
		{
			var langs = await Task.Factory.FromAsync<MSTermSvc.Languages>(((MSTermSvc.Terminology)service).BeginGetLanguages, ((MSTermSvc.Terminology)service).EndGetLanguages, null);
			var dict = new Dictionary<string, string>(langs.Count);
			foreach (MSTermSvc.Language item in langs)
			{
				string cname = item.Code;
				try { System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(item.Code); cname = ci.DisplayName; }
				catch { }
				dict.Add(item.Code, cname);
			}
			return dict;
		}

		public async Task<string> TranslateAsync(string sourceLang, string destLang, string value, InputFormat format = InputFormat.Text)
		{
			try
			{
				if (string.IsNullOrEmpty(value.Trim())) return string.Empty;
				var matches = await Task<MSTermSvc.Matches>.Factory.FromAsync(((MSTermSvc.Terminology)service).BeginGetTranslations(value, sourceLang, destLang, 
					MSTermSvc.SearchStringComparison.CaseInsensitive, MSTermSvc.SearchOperator.Exact, sources, true, 1, false, null, null, null), ((MSTermSvc.Terminology)service).EndGetTranslations);
				return matches.Count > 0 && matches[0].Translations.Count > 0 ? matches[0].Translations[0].TranslatedText : string.Empty;
			}
			catch { }
			return string.Empty;
		}

		public async Task<string> DetectLanguageAsync(string value)
		{
			throw new NotImplementedException();
		}

		public Task<T> CallMethodAsync<T>(string methodName, params string[] parameters) where T : class
		{
			throw new NotImplementedException();
		}
	}
}
